package com.sasken.pdb.controllers.remoting
{
	
	/**
	 *  This simple class is passed to a AsyncToken as one of its parameters.
	 *  It enables us to keep track of how many items have been loaded and 
	 *  when all loading is done, we can call a callback function if it was defined. 
	 * 
	 *  The way to load data from multiple asynchronous sources with this method is simply 
	 *  to pass the same tracked item to all async loaders after its initialization.  
	 */ 
	public class AsyncTrackedItem
	{
		
		public var totalItemsToLoad:int;
		private var loadedItems:int;
		public var onFinishCallback:Function;
		public var callbackParam:Object;
		
		public function AsyncTrackedItem()
		{
			loadedItems = 0;	
		}
		
		public function trackProgress():void
		{
			loadedItems++;
			if (loadedItems == totalItemsToLoad)
			{
				if (onFinishCallback is Function) 
				{
					if (callbackParam)
					{
						onFinishCallback(callbackParam);
					}
					else
					{
						onFinishCallback();
					}
				}
				return;
			}
			return;
		}

	}
}